package com.tbit.main.remote.um;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.client.exception.OceanException;
import com.tbit.main.pojo.WeiOss;
import com.tbit.main.util.HttpClientUtil;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.StringUtil;
import com.umeng.umini.param.UmengUminiGetOverviewParam;
import com.umeng.umini.param.UmengUminiGetOverviewResult;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 友盟
 *
 * @author LMY
 * @create 2022-11-21 15:35
 */
@Slf4j
public class UMHttpClient {

    public static WeiOss getAppKey(String appId) throws Exception {
        String data = HttpClientUtil.httpGet("https://mp.uqbike.cn/brandConfig/" + appId + ".json");
        return JSON.parseObject(data, WeiOss.class);
    }

    public static Long getSecond(String time) {
        List<Integer> timeList = StringUtil.getIntegerList(time, ":");
        return (long) timeList.get(0) * 3600 + timeList.get(1) * 60 + timeList.get(2);
    }

    public static void main(String[] args) throws Exception {
        WeiOss weiOss = getAppKey("2019101068331009");
        // 请替换apiKey和apiSecurity
        ApiExecutor apiExecutor = new ApiExecutor("5538284", "vK8ItHOV6Fnt");
        apiExecutor.setServerHost("gateway.open.umeng.com");
        UMHttpClient testOpenAPI = new UMHttpClient();
        System.out.println(testOpenAPI.umengUminiGetCustomerSourceOverview(apiExecutor, weiOss.getUm_appKey(), "2022-11-25", "2022-11-25"));
    }

    public List<UMData> umengUminiGetCustomerSourceOverview(ApiExecutor apiExecutor, String dataSourceId, String startDate, String endDate) {
        UmengUminiGetOverviewParam param = new UmengUminiGetOverviewParam();
        param.setDataSourceId(dataSourceId);
        param.setFromDate(startDate);
        param.setToDate(endDate);
        param.setTimeUnit("day");
        param.setIndicators("newUser,activeUser,launch,visitTimes,onceDuration,dailyDuration");
        try {
            UmengUminiGetOverviewResult result = apiExecutor.execute(param);
            List<UMData> umDatas = JSON.parseArray(JSONObject.toJSONString(result.getData().getData()), UMData.class);
            return umDatas;
        } catch (OceanException e) {
            log.error(e.getMessage(), e);
            LogUtil.error("获取友盟接口报错", e);
        }
        return null;
    }
}
